var flag = false; 
var _currentIndex = 0;
//页面初始加载
efform_onload = function() {
	$('#ef_tab_y').hide(); 
}; 
//TAB切换
function changeTab(indexclicked)
{
	var info = new EiInfo();
	info.set("fdProduceplanId",$('#fdProduceplanId').val()); 
	info.set("fdProfessionalplanId",$('#fdProfessionalplanId').val()); 
	info.set("fdPlanYear",$('#_planYear').val()); 
	info.set("fdPlanMonth",$('#_planMonth').val());  
	if(indexclicked ==0)
	{  
		EiCommunicator.send("ZMCP15", "query", info, ajax_zc_callback);
	}else if(indexclicked ==1)
	{ 
		EiCommunicator.send("ZMCP16", "query", info, ajax_zk_callback);
	}else if(indexclicked ==2)
	{ 
		EiCommunicator.send("ZMCP17", "query", info, ajax_glaz_callback);
	}  
}

function fundiv(currentIndex,index){  
	_currentIndex=index;
	changeTab(index);
}

/**回调*/
//查询钻尺回调
var ajax_zc_callback = {
	onSuccess : function(eiInfo) {
		efwindow.hide();
		var grid = efgrid.getGridObject("ef_grid_zc");
		grid.refresh(eiInfo);
	},
	onFail : function(eMsg) {
		EFAlert("新增失败，原因：" + eMsg);
	}
}
//查询钻孔回调
var ajax_zk_callback = {
	onSuccess : function(eiInfo) {
		efwindow.hide();
		var grid = efgrid.getGridObject("ef_grid_zk");
		grid.refresh(eiInfo);
	},
	onFail : function(eMsg) {
		EFAlert("新增失败，原因：" + eMsg);
	}
}
//查询管路安装回调
var ajax_glaz_callback = {
	onSuccess : function(eiInfo) {
		efwindow.hide();
		var grid = efgrid.getGridObject("ef_grid_glaz");
		grid.refresh(eiInfo);
	},
	onFail : function(eMsg) {
		EFAlert("新增失败，原因：" + eMsg);
	}
}

/****************************************************************************
 * 主表操作 查询
 *****************************************************************************/
//主表查询
button_query_onclick = function () 
{   	
	efgrid.submitInqu( "ef_grid_result", "ZM","ZMCP14","query");
} 
//主表修改
button_update_onclick = function() {
	if (isSelected("ef_grid_result") == 0) {
		return;
	}
	efgrid.submitForm("ef_grid_result", "ZM", "ZMCP14", "update", true);
} 
//主表新增
button_insert_onclick = function() {
	
	if (isSelected("ef_grid_result") == 0) {
		return;
	}
	efgrid.submitForm("ef_grid_result", "ZM", "ZMCP14", "insert", true);
} 
//主表删除
button_delete_onclick = function() {
	var grid = isSelected("ef_grid_result");
	if (grid == 0) {
		return;
	}
	var rows = grid.getCheckedRows();
	for (var i = 0; i < rows.length; i++) {
		if (grid.getCellValueByColumnName(rows[i], "fdPlanStatus") != "草拟") {
			EFAlert("只能对草拟的记录进行删除！");
			return;
		}
	}
	EFConfirm('你确认要删除吗?', '确认框',
	function(r) {
		if (r)
			efgrid.submitForm("ef_grid_result", "ZM", "ZMCP14","delete", true);
	});
} 

//上报按钮事件
button_report_onclick = function() {
	var grid = isSelected("ef_grid_result");
	if (grid == 0) {
		return;
	}
	var rows = grid.getCheckedRows();
	for (var i = 0; i < rows.length; i++) {
		if (grid.getCellValueByColumnName(rows[i], "fdPlanStatus") == "上报") {
			EFAlert("已经上报的记录，不能再次上报！");
			return;
		}
	}
	EFConfirm('你确认要上报吗?', '确认框',
			function(r) {
				if (r)
					efgrid.submitForm("ef_grid_result", "ZM", "ZMCP14", "report", true);
			});
}
/**
 * 点击主表记录 
 * @param grid_id
 * @param row_index
 */
efgrid_onRowClicked = function(grid_id, row_index) { 

	if (grid_id == "ef_grid_result") {
		var grid = efgrid.getGridObject(grid_id);
		var fdProfessionalplanId = grid.getCellValueByColumnName(row_index,	"fdKeyId");
		var fdPlanYear = grid.getCellValueByColumnName(row_index, "fdPlanYear");
		var fdPlanMonth = grid.getCellValueByColumnName(row_index, "fdPlanMonth");
		var fdPlanStatus = grid.getCellValueByColumnName(row_index, "fdPlanStatus");
		// 隐藏/显示从表按钮
		if (fdPlanStatus != '草拟') {
			hideAndShowSubButtons(false);
		} else {
			hideAndShowSubButtons(true);
		} 
		$('#fdProfessionalplanId').val(fdProfessionalplanId);
		$('#fdPlanYear').val(fdPlanYear);
		$('#fdPlanMonth').val(fdPlanMonth);
		$('#fdPlanStatus').val(fdPlanStatus); 
		changeTab(_currentIndex);
		$('#ef_tab_y').show();
	}
}



/************************************************************************************
 * 钻尺从表操作
 *************************************************************************************/
button_insertdetailzc_onclick = function() {
	if (isSelected("ef_grid_zc") == 0) return;
	efgrid.submitForm("ef_grid_zc", "ZM", "ZMCP15", "insert", true);
} 
button_updatedetailzc_onclick = function() {
	if (isSelected("ef_grid_zc") == 0) return; 
	efgrid.submitForm("ef_grid_zc", "ZM", "ZMCP15", "update", true);
} 
button_deletedetailzc_onclick = function() {
	var grid = isSelected("ef_grid_zc");
	if (grid == 0) return;
	EFConfirm('你确认要删除吗?', '确认框', function(r) {
		if (r) {
			efgrid.submitForm("ef_grid_zc", "ZM", "ZMCP15", "delete", true);
		}
	});
} 
/********************************************************************************
 * 钻孔从表操作
 *********************************************************************************/
button_insertdetailzk_onclick = function() {
	if (isSelected("ef_grid_zk") == 0) {
		return;
	}
	efgrid.submitForm("ef_grid_zk", "ZM", "ZMCP16", "insert", true);
} 
button_updatedetailzk_onclick = function() {
	if (isSelected("ef_grid_zk") == 0) {
		return;
	}
	efgrid.submitForm("ef_grid_zk", "ZM", "ZMCP16", "update", true);
}
button_deletedetailzk_onclick = function() {
	var grid = isSelected("ef_grid_zk");
	if (grid == 0) {
		return;
	}
	EFConfirm('你确认要删除吗?', '确认框', function(r) {
		if (r) {
			efgrid.submitForm("ef_grid_zk", "ZM", "ZMCP16", "delete", true);
		}
	});
} 

/********************************************************************************
 * 管路安装从表操作 
 *********************************************************************************/
button_insertdetailglaz_onclick = function() {
	if (isSelected("ef_grid_glaz") == 0) {
		return;
	}
	efgrid.submitForm("ef_grid_glaz", "ZM", "ZMCP17", "insert", true);
} 
button_updatedetailglaz_onclick = function() {
	if (isSelected("ef_grid_glaz") == 0) {
		return;
	}
	efgrid.submitForm("ef_grid_glaz", "ZM", "ZMCP17", "update", true);
} 
button_deletedetailglaz_onclick = function() {
	var grid = isSelected("ef_grid_glaz");
	if (grid == 0) {
		return;
	}
	EFConfirm('你确认要删除吗?', '确认框', function(r) {
		if (r) {
			efgrid.submitForm("ef_grid_glaz", "ZM", "ZMCP17", "delete", true);
		}
	});
} 
/**
 * 从表按钮显示
 */
var hideAndShowSubButtons = function(status) {

	if (!status) {
		efbutton.setButtonStatus("insertdetailzc", false);
		efbutton.setButtonStatus("updatedetailzc", false);
		efbutton.setButtonStatus("deletedetailzc", false);
		efbutton.setButtonStatus("insertdetailzk", false);
		efbutton.setButtonStatus("updatedetailzk", false);
		efbutton.setButtonStatus("deletedetailzk", false);
		efbutton.setButtonStatus("insertdetailglaz", false);
		efbutton.setButtonStatus("updatedetailglaz", false);
		efbutton.setButtonStatus("deletedetailglaz", false);
	} else {
		efbutton.setButtonStatus("insertdetailzc", true);
		efbutton.setButtonStatus("updatedetailzc", true);
		efbutton.setButtonStatus("deletedetailzc", true);
		efbutton.setButtonStatus("insertdetailzk", true);
		efbutton.setButtonStatus("updatedetailzk", true);
		efbutton.setButtonStatus("deletedetailzk", true);
		efbutton.setButtonStatus("insertdetailglaz", true);
		efbutton.setButtonStatus("updatedetailglaz", true);
		efbutton.setButtonStatus("deletedetailglaz", true);
	}
}
//新增记录行回调函数
efgrid_afterAddNewRow = function(grid_id){ 

	var grid = efgrid.getGridObject(grid_id);
	var rows = grid.getCheckedRows();	
	if(grid_id=="ef_grid_result")
	{
		var fdCreator=$('#fdCreator').val();
		var fdCreateDepartment=$('#fdCreateDepartment').val();
		var fdCreatorId=$('#fdCreatorId').val();
		var fdCreateDepartmentId=$('#fdCreateDepartmentId').val(); 
		var now = new Date();
	    var year = now.getFullYear();       //年
	    var month = now.getMonth() + 1;     //月
	    if(month > 12) {
	    	month = 1;
	    	year += 1;
	    }
	    if(month < 10) month = "0"+month;
	    var day = now.getDate();            //日 
	    var clock = year + "-" + month + "-";
	    if(day < 10) clock += "0";
	    clock += day;
	   
		for(var i = 0 ; i < rows.length;i++){
			grid.setCellValue(rows[i],0,"草拟",TYPE_DATA); 
			grid.setCellValue(rows[i],1,year,TYPE_DATA);
			grid.setCellValue(rows[i],2,month,TYPE_DATA);
			grid.setCellValue(rows[i],3,year+"年"+month+"月的抽采计划",TYPE_DATA);
			grid.setCellValue(rows[i],4,"抽采计划",TYPE_DATA);
			grid.setCellValue(rows[i],5,clock,TYPE_DATA);
			grid.setCellValue(rows[i],6,0,TYPE_DATA); 
			grid.setCellValue(rows[i],7,0,TYPE_DATA); 
			grid.setCellValue(rows[i],8,0,TYPE_DATA); 
			grid.setCellValue(rows[i],9,0,TYPE_DATA); 
			grid.setCellValue(rows[i],10,0,TYPE_DATA); 
			grid.setCellValue(rows[i],11,0,TYPE_DATA); 
			grid.setCellValue(rows[i],12,0,TYPE_DATA);
		}
	}else if(grid_id=="ef_grid_zc")
	{
		for(var i = 0 ; i < rows.length;i++){
			
			grid.setCellValue(rows[i],2,0,TYPE_DATA); 
			grid.setCellValue(rows[i],3,0,TYPE_DATA); 
			grid.setCellValue(rows[i],4,0,TYPE_DATA); 
			grid.setCellValue(rows[i],6,0,TYPE_DATA); 
			grid.setCellValue(rows[i],7,0,TYPE_DATA); 
			grid.setCellValue(rows[i],8,0,TYPE_DATA); 
		}
	}
} 
//提交成功回调函数
efgrid_onAjaxSubmitSuccess = function(gridId, service_name, method_name, eiInfo) {
	if(gridId == "ef_grid_zk" || gridId == "ef_grid_zc" || gridId == "ef_grid_glaz") {
		if (eiInfo.status != -1) {
			var grid = efgrid.getGridObject(gridId);
			grid.refresh( eiInfo );
		}
		else
		{
			submitCallBackEiMsg = eiInfo.msg;
		}
	} else{
		if (eiInfo.status != -1) {
			var grid = efgrid.getGridObject(gridId);
			grid.refresh( eiInfo );
			$('#ef_tab_y').hide();
		}
		else
		{
			submitCallBackEiMsg = eiInfo.msg;
			flag = true;
		}
	}
};

//单元格编辑后回调的方法
efgrid_onDataCellSaved = function( grid_id, row_index, col_index, cell_value ) 
{
	if(grid_id == "ef_grid_result"){
		  var grid = efgrid.getGridObject( grid_id ); 
		  if( col_index == 6 || col_index == 8) 
		  { 
			  var ymcl = grid.getCellValueByColumnName(row_index,"fdYmSum");//原煤产量
			  var dmccl = grid.getCellValueByColumnName(row_index,"fdDmccl");//吨煤瓦斯抽采量
			  grid.setCellValue(row_index,7,Math.round(Number(ymcl)*Number(dmccl)),TYPE_DATA);
			  grid.refreshCell(row_index,7,TYPE_DATA );
		  } 
		  }
	if(grid_id == "ef_grid_zc"){
	  var grid = efgrid.getGridObject( grid_id ); 
	  if( col_index == 6 || col_index == 7 || col_index == 8) 
	  { 
		  var sx = grid.getCellValueByColumnName(row_index,"fdPlanSx");
		  var zx = grid.getCellValueByColumnName(row_index,"fdPlanZx");
		  var xx = grid.getCellValueByColumnName(row_index,"fdPlanXx");
		  grid.setCellValue(row_index,2,Number(sx)+Number(zx)+Number(xx),TYPE_DATA);
		  grid.refreshCell( row_index, 2 , TYPE_DATA ); 
	  } 
	  }
	if(grid_id == "ef_grid_zk"){
		  var grid = efgrid.getGridObject( grid_id ); 
		  if( col_index == 3 || col_index == 4) 
		  { 
			  var sl = grid.getCellValueByColumnName(row_index,"fdHoleLength");//数量
			  var ks = grid.getCellValueByColumnName(row_index,"fdKs");//孔深(米)
			  grid.setCellValue(row_index,5,Number(sl)*Number(ks),TYPE_DATA);
			  grid.refreshCell( row_index, 5 , TYPE_DATA ); 
		  } 
		  }
}